<?php 

class App_Model_DbTable_Article_Attachment_Video_Data extends Ext_Db_Table  
{  
    protected $_name = 'article_attachment_video_data';
    
    private static $_videoDataRow = array();
    
    public function getRowById($id)
    {
        if(!$id)
        {
                return "";
        }

        if(isset(self::$_videoDataRow['id'][$id]))
        {
                return self::$_videoDataRow['id'][$id];
        }

        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $id);
        $row = $this->fetchRow($safe);
        if (!$row) {
                //throw new Exception("Count not find row $id");
                return "";
        }
        $_videoDataRow = $row->toArray();

        self::$_videoDataRow['id'][$id] = $_videoDataRow;

        return $_videoDataRow;
    }

    public function getAllById($idArray)
    {
        if(!$idArray)
        {
                return "";
        }

        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id IN(?) ", $idArray);
        $all = $this->fetchAll($safe);
        if (!$all) {
                //throw new Exception("Count not find row $id");
                return "";
        }
        return $all->toArray();
    }

    public function del($id)
    {
        if(!$id)
        {
                return "";
        }

        $this->_unsetVideoDataRow($id);

        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $id);
        $rows_affected = $this->delete($safe);

        return $rows_affected;
    }

    public function addNew($inputArray)
    {
        $sql_data_array = array();
        if(array_key_exists('articleId', $inputArray)) $sql_data_array['articleId'] = $inputArray['articleId'];
        if(array_key_exists('videoId', $inputArray)) $sql_data_array['videoId'] = $inputArray['videoId'];
        if(array_key_exists('title', $inputArray)) $sql_data_array['title'] = $inputArray['title'];
        if(array_key_exists('description', $inputArray)) $sql_data_array['description'] = $inputArray['description'];
        if(array_key_exists('htmlOn', $inputArray)) $sql_data_array['htmlOn'] = $inputArray['htmlOn'];

        try {
                // 插入数据行并返回插入的行数
                $rows_affected = $this->insert($sql_data_array);
                $dataId = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
                trigger_error($e->getMessage(), E_USER_WARNING);
        }

        return $dataId;	
    }

    public function addOld($inputArray, $_videoDataId)
    {
        $sql_data_array = array();
        if(array_key_exists('description', $inputArray)) $sql_data_array['description'] = $inputArray['description'];
        if(array_key_exists('htmlOn', $inputArray)) $sql_data_array['htmlOn'] = $inputArray['htmlOn'];
        if(!$sql_data_array)
        {
                return "";
        }

        try {
                $db = $this->getAdapter();
                $where = $db->quoteInto(" id = ? ", $_videoDataId);
                $rows_affected = $this->update($sql_data_array, $where);
                if($rows_affected){
                    $this->_unsetVideoDataRow($_videoDataId);
                }
        }catch (Exception $e){
                trigger_error($e->getMessage(), E_USER_WARNING);
        }

        return "";
    }
	
    private function _unsetVideoDataRow($_videoDataId)
    {
        if(!$_videoDataId)
        {
            return "";
        }
        
        $_videoDataRow = $this->getRowById($_videoDataId);
        if(!$_videoDataRow)
        {
            return "";
        }
        
        unset(self::$_videoDataRow['id'][$_videoDataId]);
    }
}